﻿using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Runtime.CompilerServices;
using System.Text;
using System.Threading.Tasks;
using HslCommunication.BasicFramework;
using HslCommunication.Core;
using HslCommunication.Core.Net;
using HslCommunication.Reflection;

namespace HslCommunication.Profinet.LSIS
{
	/// <summary>
	/// XGB Cnet I/F module supports Serial Port. On Tcp/ip implementation, The address can carry station number information, for example: s=2;D100
	/// </summary>
	/// <remarks>
	/// Address example likes the follow
	/// <list type="table">
	///   <listheader>
	///     <term>地址名称</term>
	///     <term>地址代号</term>
	///     <term>示例</term>
	///     <term>地址进制</term>
	///     <term>字操作</term>
	///     <term>位操作</term>
	///     <term>备注</term>
	///   </listheader>
	///   <item>
	///     <term>*</term>
	///     <term>P</term>
	///     <term>PX100,PB100,PW100,PD100,PL100</term>
	///     <term>10</term>
	///     <term>√</term>
	///     <term>√</term>
	///     <term></term>
	///   </item>
	///   <item>
	///     <term>*</term>
	///     <term>M</term>
	///     <term>MX100,MB100,MW100,MD100,ML100</term>
	///     <term>10</term>
	///     <term>√</term>
	///     <term>√</term>
	///     <term></term>
	///   </item>
	///   <item>
	///     <term>*</term>
	///     <term>L</term>
	///     <term>LX100,LB100,LW100,LD100,LL100</term>
	///     <term>10</term>
	///     <term>√</term>
	///     <term>√</term>
	///     <term></term>
	///   </item>
	///   <item>
	///     <term>*</term>
	///     <term>K</term>
	///     <term>KX100,KB100,KW100,KD100,KL100</term>
	///     <term>10</term>
	///     <term>√</term>
	///     <term>√</term>
	///     <term></term>
	///   </item>
	///   <item>
	///     <term>*</term>
	///     <term>F</term>
	///     <term>FX100,FB100,FW100,FD100,FL100</term>
	///     <term>10</term>
	///     <term>√</term>
	///     <term>√</term>
	///     <term></term>
	///   </item>
	///   <item>
	///     <term></term>
	///     <term>T</term>
	///     <term>TX100,TB100,TW100,TD100,TL100</term>
	///     <term>10</term>
	///     <term>√</term>
	///     <term>√</term>
	///     <term></term>
	///   </item>
	///   <item>
	///     <term></term>
	///     <term>C</term>
	///     <term>CX100,CB100,CW100,CD100,CL100</term>
	///     <term>10</term>
	///     <term>√</term>
	///     <term>√</term>
	///     <term></term>
	///   </item>
	///   <item>
	///     <term></term>
	///     <term>D</term>
	///     <term>DX100,DB100,DW100,DD100,DL100</term>
	///     <term>10</term>
	///     <term>√</term>
	///     <term>√</term>
	///     <term></term>
	///   </item>
	///   <item>
	///     <term></term>
	///     <term>S</term>
	///     <term>SX100,SB100,SW100,SD100,SL100</term>
	///     <term>10</term>
	///     <term>√</term>
	///     <term>√</term>
	///     <term></term>
	///   </item>
	///   <item>
	///     <term></term>
	///     <term>Q</term>
	///     <term>QX100,QB100,QW100,QD100,QL100</term>
	///     <term>10</term>
	///     <term>√</term>
	///     <term>√</term>
	///     <term></term>
	///   </item>
	///   <item>
	///     <term></term>
	///     <term>I</term>
	///     <term>IX100,IB100,IW100,ID100,IL100</term>
	///     <term>10</term>
	///     <term>√</term>
	///     <term>√</term>
	///     <term></term>
	///   </item>
	///   <item>
	///     <term></term>
	///     <term>N</term>
	///     <term>NX100,NB100,NW100,ND100,NL100</term>
	///     <term>10</term>
	///     <term>√</term>
	///     <term>√</term>
	///     <term></term>
	///   </item>
	///   <item>
	///     <term></term>
	///     <term>U</term>
	///     <term>UX100,UB100,UW100,UD100,UL100</term>
	///     <term>10</term>
	///     <term>√</term>
	///     <term>√</term>
	///     <term></term>
	///   </item>
	///   <item>
	///     <term></term>
	///     <term>Z</term>
	///     <term>ZX100,ZB100,ZW100,ZD100,ZL100</term>
	///     <term>10</term>
	///     <term>√</term>
	///     <term>√</term>
	///     <term></term>
	///   </item>
	///   <item>
	///     <term></term>
	///     <term>R</term>
	///     <term>RX100,RB100,RW100,RD100,RL100</term>
	///     <term>10</term>
	///     <term>√</term>
	///     <term>√</term>
	///     <term></term>
	///   </item>
	/// </list>
	/// </remarks>
	// Token: 0x02000083 RID: 131
	public class XGBCnetOverTcp : NetworkDeviceBase
	{
		/// <summary>
		/// Instantiate a Default object
		/// </summary>
		// Token: 0x060009EB RID: 2539 RVA: 0x0003AD5D File Offset: 0x00038F5D
		public XGBCnetOverTcp()
		{
			base.WordLength = 2;
			base.ByteTransform = new RegularByteTransform();
			base.SleepTime = 20;
		}

		/// <summary>
		/// PLC Station No.
		/// </summary>
		// Token: 0x170001A4 RID: 420
		// (get) Token: 0x060009EC RID: 2540 RVA: 0x0003AD8B File Offset: 0x00038F8B
		// (set) Token: 0x060009ED RID: 2541 RVA: 0x0003AD93 File Offset: 0x00038F93
		public byte Station { get; set; } = 5;

		/// <summary>
		/// Read single byte value from plc
		/// </summary>
		/// <param name="address">Start address</param>
		/// <returns>result</returns>
		// Token: 0x060009EE RID: 2542 RVA: 0x0003AD9C File Offset: 0x00038F9C
		[HslMqttApi("ReadByte", "")]
		public OperateResult<byte> ReadByte(string address)
		{
			return ByteTransformHelper.GetResultFromArray<byte>(this.Read(address, 2));
		}

		/// <summary>
		/// Write single byte value to plc
		/// </summary>
		/// <param name="address">Start address</param>
		/// <param name="value">value</param>
		/// <returns>Whether to write the successful</returns>
		// Token: 0x060009EF RID: 2543 RVA: 0x0001BA2D File Offset: 0x00019C2D
		[HslMqttApi("WriteByte", "")]
		public OperateResult Write(string address, byte value)
		{
			return this.Write(address, new byte[]
			{
				value
			});
		}

		/// <summary>
		/// Read single byte value from plc
		/// </summary>
		/// <param name="address">Start address</param>
		/// <returns>read result</returns>
		// Token: 0x060009F0 RID: 2544 RVA: 0x0003ADAC File Offset: 0x00038FAC
		[DebuggerStepThrough]
		public Task<OperateResult<byte>> ReadByteAsync(string address)
		{
			XGBCnetOverTcp.<ReadByteAsync>d__7 <ReadByteAsync>d__ = new XGBCnetOverTcp.<ReadByteAsync>d__7();
			<ReadByteAsync>d__.<>t__builder = AsyncTaskMethodBuilder<OperateResult<byte>>.Create();
			<ReadByteAsync>d__.<>4__this = this;
			<ReadByteAsync>d__.address = address;
			<ReadByteAsync>d__.<>1__state = -1;
			<ReadByteAsync>d__.<>t__builder.Start<XGBCnetOverTcp.<ReadByteAsync>d__7>(ref <ReadByteAsync>d__);
			return <ReadByteAsync>d__.<>t__builder.Task;
		}

		/// <summary>
		/// Write single byte value to plc
		/// </summary>
		/// <param name="address">Start address</param>
		/// <param name="value">value</param>
		/// <returns>Whether to write the successful</returns>
		// Token: 0x060009F1 RID: 2545 RVA: 0x0003ADF8 File Offset: 0x00038FF8
		[DebuggerStepThrough]
		public Task<OperateResult> WriteAsync(string address, byte value)
		{
			XGBCnetOverTcp.<WriteAsync>d__8 <WriteAsync>d__ = new XGBCnetOverTcp.<WriteAsync>d__8();
			<WriteAsync>d__.<>t__builder = AsyncTaskMethodBuilder<OperateResult>.Create();
			<WriteAsync>d__.<>4__this = this;
			<WriteAsync>d__.address = address;
			<WriteAsync>d__.value = value;
			<WriteAsync>d__.<>1__state = -1;
			<WriteAsync>d__.<>t__builder.Start<XGBCnetOverTcp.<WriteAsync>d__8>(ref <WriteAsync>d__);
			return <WriteAsync>d__.<>t__builder.Task;
		}

		/// <inheritdoc />
		// Token: 0x060009F2 RID: 2546 RVA: 0x0003AE4C File Offset: 0x0003904C
		[HslMqttApi("ReadBoolArray", "")]
		public override OperateResult<bool[]> ReadBool(string address, ushort length)
		{
			byte station = (byte)HslHelper.ExtractParameter(ref address, "s", (int)this.Station);
			OperateResult<byte[]> operateResult = XGBCnetOverTcp.BuildReadOneCommand(station, address, length);
			bool flag = !operateResult.IsSuccess;
			OperateResult<bool[]> result;
			if (flag)
			{
				result = OperateResult.CreateFailedResult<bool[]>(operateResult);
			}
			else
			{
				OperateResult<byte[]> operateResult2 = base.ReadFromCoreServer(operateResult.Content);
				bool flag2 = !operateResult2.IsSuccess;
				if (flag2)
				{
					result = OperateResult.CreateFailedResult<bool[]>(operateResult2);
				}
				else
				{
					result = OperateResult.CreateSuccessResult<bool[]>(SoftBasic.ByteToBoolArray(XGBCnetOverTcp.ExtractActualData(operateResult2.Content, true).Content, (int)length));
				}
			}
			return result;
		}

		/// <summary>
		/// ReadCoil, same as ReadBool
		/// </summary>
		/// <param name="address">address, for example: MX100, PX100</param>
		/// <returns>Result</returns>
		// Token: 0x060009F3 RID: 2547 RVA: 0x0003AED6 File Offset: 0x000390D6
		public OperateResult<bool> ReadCoil(string address)
		{
			return this.ReadBool(address);
		}

		/// <summary>
		/// ReadCoil, same as ReadBool
		/// </summary>
		/// <param name="address">address, for example: MX100, PX100</param>
		/// <param name="length">array length</param>
		/// <returns>result</returns>
		// Token: 0x060009F4 RID: 2548 RVA: 0x0003AEDF File Offset: 0x000390DF
		public OperateResult<bool[]> ReadCoil(string address, ushort length)
		{
			return this.ReadBool(address, length);
		}

		/// <summary>
		/// WriteCoil
		/// </summary>
		/// <param name="address">Start Address</param>
		/// <param name="value">value for write</param>
		/// <returns>whether write is success</returns>
		// Token: 0x060009F5 RID: 2549 RVA: 0x0003AEE9 File Offset: 0x000390E9
		public OperateResult WriteCoil(string address, bool value)
		{
			return this.Write(address, value);
		}

		/// <inheritdoc />
		// Token: 0x060009F6 RID: 2550 RVA: 0x0003AEF3 File Offset: 0x000390F3
		[HslMqttApi("WriteBool", "")]
		public override OperateResult Write(string address, bool value)
		{
			return this.Write(address, new byte[]
			{
				value ? 1 : 0
			});
		}

		/// <inheritdoc />
		// Token: 0x060009F7 RID: 2551 RVA: 0x0003AF10 File Offset: 0x00039110
		[DebuggerStepThrough]
		public override Task<OperateResult<bool[]>> ReadBoolAsync(string address, ushort length)
		{
			XGBCnetOverTcp.<ReadBoolAsync>d__14 <ReadBoolAsync>d__ = new XGBCnetOverTcp.<ReadBoolAsync>d__14();
			<ReadBoolAsync>d__.<>t__builder = AsyncTaskMethodBuilder<OperateResult<bool[]>>.Create();
			<ReadBoolAsync>d__.<>4__this = this;
			<ReadBoolAsync>d__.address = address;
			<ReadBoolAsync>d__.length = length;
			<ReadBoolAsync>d__.<>1__state = -1;
			<ReadBoolAsync>d__.<>t__builder.Start<XGBCnetOverTcp.<ReadBoolAsync>d__14>(ref <ReadBoolAsync>d__);
			return <ReadBoolAsync>d__.<>t__builder.Task;
		}

		/// <inheritdoc cref="M:HslCommunication.Profinet.LSIS.XGBCnetOverTcp.ReadCoil(System.String)" />
		// Token: 0x060009F8 RID: 2552 RVA: 0x0003AF64 File Offset: 0x00039164
		[DebuggerStepThrough]
		public Task<OperateResult<bool>> ReadCoilAsync(string address)
		{
			XGBCnetOverTcp.<ReadCoilAsync>d__15 <ReadCoilAsync>d__ = new XGBCnetOverTcp.<ReadCoilAsync>d__15();
			<ReadCoilAsync>d__.<>t__builder = AsyncTaskMethodBuilder<OperateResult<bool>>.Create();
			<ReadCoilAsync>d__.<>4__this = this;
			<ReadCoilAsync>d__.address = address;
			<ReadCoilAsync>d__.<>1__state = -1;
			<ReadCoilAsync>d__.<>t__builder.Start<XGBCnetOverTcp.<ReadCoilAsync>d__15>(ref <ReadCoilAsync>d__);
			return <ReadCoilAsync>d__.<>t__builder.Task;
		}

		/// <inheritdoc cref="M:HslCommunication.Profinet.LSIS.XGBCnetOverTcp.ReadCoil(System.String,System.UInt16)" />
		// Token: 0x060009F9 RID: 2553 RVA: 0x0003AFB0 File Offset: 0x000391B0
		[DebuggerStepThrough]
		public Task<OperateResult<bool[]>> ReadCoilAsync(string address, ushort length)
		{
			XGBCnetOverTcp.<ReadCoilAsync>d__16 <ReadCoilAsync>d__ = new XGBCnetOverTcp.<ReadCoilAsync>d__16();
			<ReadCoilAsync>d__.<>t__builder = AsyncTaskMethodBuilder<OperateResult<bool[]>>.Create();
			<ReadCoilAsync>d__.<>4__this = this;
			<ReadCoilAsync>d__.address = address;
			<ReadCoilAsync>d__.length = length;
			<ReadCoilAsync>d__.<>1__state = -1;
			<ReadCoilAsync>d__.<>t__builder.Start<XGBCnetOverTcp.<ReadCoilAsync>d__16>(ref <ReadCoilAsync>d__);
			return <ReadCoilAsync>d__.<>t__builder.Task;
		}

		/// <inheritdoc cref="M:HslCommunication.Profinet.LSIS.XGBCnetOverTcp.WriteCoil(System.String,System.Boolean)" />
		// Token: 0x060009FA RID: 2554 RVA: 0x0003B004 File Offset: 0x00039204
		[DebuggerStepThrough]
		public Task<OperateResult> WriteCoilAsync(string address, bool value)
		{
			XGBCnetOverTcp.<WriteCoilAsync>d__17 <WriteCoilAsync>d__ = new XGBCnetOverTcp.<WriteCoilAsync>d__17();
			<WriteCoilAsync>d__.<>t__builder = AsyncTaskMethodBuilder<OperateResult>.Create();
			<WriteCoilAsync>d__.<>4__this = this;
			<WriteCoilAsync>d__.address = address;
			<WriteCoilAsync>d__.value = value;
			<WriteCoilAsync>d__.<>1__state = -1;
			<WriteCoilAsync>d__.<>t__builder.Start<XGBCnetOverTcp.<WriteCoilAsync>d__17>(ref <WriteCoilAsync>d__);
			return <WriteCoilAsync>d__.<>t__builder.Task;
		}

		/// <inheritdoc cref="M:HslCommunication.Profinet.LSIS.XGBCnetOverTcp.WriteCoil(System.String,System.Boolean)" />
		// Token: 0x060009FB RID: 2555 RVA: 0x0003B058 File Offset: 0x00039258
		[DebuggerStepThrough]
		public override Task<OperateResult> WriteAsync(string address, bool value)
		{
			XGBCnetOverTcp.<WriteAsync>d__18 <WriteAsync>d__ = new XGBCnetOverTcp.<WriteAsync>d__18();
			<WriteAsync>d__.<>t__builder = AsyncTaskMethodBuilder<OperateResult>.Create();
			<WriteAsync>d__.<>4__this = this;
			<WriteAsync>d__.address = address;
			<WriteAsync>d__.value = value;
			<WriteAsync>d__.<>1__state = -1;
			<WriteAsync>d__.<>t__builder.Start<XGBCnetOverTcp.<WriteAsync>d__18>(ref <WriteAsync>d__);
			return <WriteAsync>d__.<>t__builder.Task;
		}

		/// <inheritdoc />
		// Token: 0x060009FC RID: 2556 RVA: 0x0003B0AC File Offset: 0x000392AC
		[HslMqttApi("ReadByteArray", "")]
		public override OperateResult<byte[]> Read(string address, ushort length)
		{
			byte station = (byte)HslHelper.ExtractParameter(ref address, "s", (int)this.Station);
			OperateResult<byte[]> operateResult = XGBCnetOverTcp.BuildReadCommand(station, address, length);
			bool flag = !operateResult.IsSuccess;
			OperateResult<byte[]> result;
			if (flag)
			{
				result = operateResult;
			}
			else
			{
				OperateResult<byte[]> operateResult2 = base.ReadFromCoreServer(operateResult.Content);
				bool flag2 = !operateResult2.IsSuccess;
				if (flag2)
				{
					result = operateResult2;
				}
				else
				{
					result = XGBCnetOverTcp.ExtractActualData(operateResult2.Content, true);
				}
			}
			return result;
		}

		/// <inheritdoc />
		// Token: 0x060009FD RID: 2557 RVA: 0x0003B11C File Offset: 0x0003931C
		[HslMqttApi("WriteByteArray", "")]
		public override OperateResult Write(string address, byte[] value)
		{
			byte station = (byte)HslHelper.ExtractParameter(ref address, "s", (int)this.Station);
			OperateResult<byte[]> operateResult = XGBCnetOverTcp.BuildWriteCommand(station, address, value);
			bool flag = !operateResult.IsSuccess;
			OperateResult result;
			if (flag)
			{
				result = operateResult;
			}
			else
			{
				OperateResult<byte[]> operateResult2 = base.ReadFromCoreServer(operateResult.Content);
				bool flag2 = !operateResult2.IsSuccess;
				if (flag2)
				{
					result = operateResult2;
				}
				else
				{
					result = XGBCnetOverTcp.ExtractActualData(operateResult2.Content, false);
				}
			}
			return result;
		}

		/// <inheritdoc />
		// Token: 0x060009FE RID: 2558 RVA: 0x0003B18C File Offset: 0x0003938C
		[DebuggerStepThrough]
		public override Task<OperateResult<byte[]>> ReadAsync(string address, ushort length)
		{
			XGBCnetOverTcp.<ReadAsync>d__21 <ReadAsync>d__ = new XGBCnetOverTcp.<ReadAsync>d__21();
			<ReadAsync>d__.<>t__builder = AsyncTaskMethodBuilder<OperateResult<byte[]>>.Create();
			<ReadAsync>d__.<>4__this = this;
			<ReadAsync>d__.address = address;
			<ReadAsync>d__.length = length;
			<ReadAsync>d__.<>1__state = -1;
			<ReadAsync>d__.<>t__builder.Start<XGBCnetOverTcp.<ReadAsync>d__21>(ref <ReadAsync>d__);
			return <ReadAsync>d__.<>t__builder.Task;
		}

		/// <inheritdoc />
		// Token: 0x060009FF RID: 2559 RVA: 0x0003B1E0 File Offset: 0x000393E0
		[DebuggerStepThrough]
		public override Task<OperateResult> WriteAsync(string address, byte[] value)
		{
			XGBCnetOverTcp.<WriteAsync>d__22 <WriteAsync>d__ = new XGBCnetOverTcp.<WriteAsync>d__22();
			<WriteAsync>d__.<>t__builder = AsyncTaskMethodBuilder<OperateResult>.Create();
			<WriteAsync>d__.<>4__this = this;
			<WriteAsync>d__.address = address;
			<WriteAsync>d__.value = value;
			<WriteAsync>d__.<>1__state = -1;
			<WriteAsync>d__.<>t__builder.Start<XGBCnetOverTcp.<WriteAsync>d__22>(ref <WriteAsync>d__);
			return <WriteAsync>d__.<>t__builder.Task;
		}

		/// <inheritdoc />
		// Token: 0x06000A00 RID: 2560 RVA: 0x0003B232 File Offset: 0x00039432
		public override string ToString()
		{
			return string.Format("XGBCnetOverTcp[{0}:{1}]", this.IpAddress, this.Port);
		}

		/// <summary>
		/// AnalysisAddress IX0.0.0 QX0.0.0  MW1.0  MB1.0
		/// </summary>
		/// <param name="address"></param>
		/// <param name="QI"></param>
		/// <returns></returns>
		// Token: 0x06000A01 RID: 2561 RVA: 0x0003B250 File Offset: 0x00039450
		public static int CalculateAddressStarted(string address, bool QI = false)
		{
			bool flag = address.IndexOf('.') < 0;
			int result;
			if (flag)
			{
				result = Convert.ToInt32(address);
			}
			else
			{
				string[] array = address.Split(new char[]
				{
					'.'
				});
				bool flag2 = !QI;
				if (flag2)
				{
					result = Convert.ToInt32(array[0]);
				}
				else
				{
					result = Convert.ToInt32(array[2]);
				}
			}
			return result;
		}

		/// <summary>
		/// NumberStyles HexNumber
		/// </summary>
		/// <param name="value"></param>
		/// <returns></returns>
		// Token: 0x06000A02 RID: 2562 RVA: 0x0003B2AC File Offset: 0x000394AC
		private static bool IsHex(string value)
		{
			bool flag = string.IsNullOrEmpty(value);
			bool result;
			if (flag)
			{
				result = false;
			}
			else
			{
				bool flag2 = false;
				int i = 0;
				while (i < value.Length)
				{
					char c = value[i];
					char c2 = c;
					switch (c2)
					{
					case 'A':
					case 'B':
					case 'C':
					case 'D':
					case 'E':
					case 'F':
						goto IL_6B;
					default:
						switch (c2)
						{
						case 'a':
						case 'b':
						case 'c':
						case 'd':
						case 'e':
						case 'f':
							goto IL_6B;
						}
						break;
					}
					IL_6F:
					i++;
					continue;
					IL_6B:
					flag2 = true;
					goto IL_6F;
				}
				result = flag2;
			}
			return result;
		}

		/// <summary>
		/// AnalysisAddress
		/// </summary>
		/// <param name="address">start address</param>
		/// <returns>analysis result</returns>
		// Token: 0x06000A03 RID: 2563 RVA: 0x0003B344 File Offset: 0x00039544
		public static OperateResult<string> AnalysisAddress(string address)
		{
			StringBuilder stringBuilder = new StringBuilder();
			try
			{
				stringBuilder.Append("%");
				char[] array = new char[]
				{
					'P',
					'M',
					'L',
					'K',
					'F',
					'T',
					'C',
					'D',
					'S',
					'Q',
					'I',
					'N',
					'U',
					'Z',
					'R'
				};
				bool flag = false;
				for (int i = 0; i < array.Length; i++)
				{
					bool flag2 = array[i] == address[0];
					if (flag2)
					{
						stringBuilder.Append(array[i]);
						char c = address[1];
						char c2 = c;
						if (c2 != 'X')
						{
							stringBuilder.Append("B");
							bool flag3 = address[1] == 'B';
							if (flag3)
							{
								int num = XGBCnetOverTcp.CalculateAddressStarted(address.Substring(2), false);
								stringBuilder.Append((num == 0) ? num : (num * 2));
							}
							else
							{
								bool flag4 = address[1] == 'W';
								if (flag4)
								{
									int num = XGBCnetOverTcp.CalculateAddressStarted(address.Substring(2), false);
									stringBuilder.Append((num == 0) ? num : (num * 2));
								}
								else
								{
									bool flag5 = address[1] == 'D';
									if (flag5)
									{
										int num = XGBCnetOverTcp.CalculateAddressStarted(address.Substring(2), false);
										stringBuilder.Append((num == 0) ? num : (num * 4));
									}
									else
									{
										bool flag6 = address[1] == 'L';
										if (flag6)
										{
											int num = XGBCnetOverTcp.CalculateAddressStarted(address.Substring(2), false);
											stringBuilder.Append((num == 0) ? num : (num * 8));
										}
										else
										{
											bool flag7 = address[0] == 'I' || address[0] == 'Q';
											if (flag7)
											{
												stringBuilder.Append(XGBCnetOverTcp.CalculateAddressStarted(address.Substring(1), true));
											}
											else
											{
												bool flag8 = XGBCnetOverTcp.IsHex(address.Substring(1));
												if (flag8)
												{
													stringBuilder.Append(address.Substring(1));
												}
												else
												{
													stringBuilder.Append(XGBCnetOverTcp.CalculateAddressStarted(address.Substring(1), false));
												}
											}
										}
									}
								}
							}
						}
						else
						{
							stringBuilder.Append("X");
							bool flag9 = address[0] == 'I' || address[0] == 'Q';
							if (flag9)
							{
								stringBuilder.Append(XGBCnetOverTcp.CalculateAddressStarted(address.Substring(2), true));
							}
							else
							{
								bool flag10 = XGBCnetOverTcp.IsHex(address.Substring(2));
								if (flag10)
								{
									stringBuilder.Append(address.Substring(2));
								}
								else
								{
									stringBuilder.Append(XGBCnetOverTcp.CalculateAddressStarted(address.Substring(2), false));
								}
							}
						}
						flag = true;
						break;
					}
				}
				bool flag11 = !flag;
				if (flag11)
				{
					throw new Exception(StringResources.Language.NotSupportedDataType);
				}
			}
			catch (Exception ex)
			{
				return new OperateResult<string>(ex.Message);
			}
			return OperateResult.CreateSuccessResult<string>(stringBuilder.ToString());
		}

		/// <summary>
		/// reading address  Type of ReadByte
		/// </summary>
		/// <param name="station">plc station</param>
		/// <param name="address">address, for example: M100, D100, DW100</param>
		/// <param name="length">read length</param>
		/// <returns>command bytes</returns>
		// Token: 0x06000A04 RID: 2564 RVA: 0x0003B628 File Offset: 0x00039828
		public static OperateResult<byte[]> BuildReadByteCommand(byte station, string address, ushort length)
		{
			OperateResult<string> operateResult = XGBCnetOverTcp.AnalysisAddress(address);
			bool flag = !operateResult.IsSuccess;
			OperateResult<byte[]> result;
			if (flag)
			{
				result = OperateResult.CreateFailedResult<byte[]>(operateResult);
			}
			else
			{
				List<byte> list = new List<byte>();
				list.Add(5);
				list.AddRange(SoftBasic.BuildAsciiBytesFrom(station));
				list.Add(114);
				list.Add(83);
				list.Add(66);
				list.AddRange(SoftBasic.BuildAsciiBytesFrom((byte)operateResult.Content.Length));
				list.AddRange(Encoding.ASCII.GetBytes(operateResult.Content));
				list.AddRange(SoftBasic.BuildAsciiBytesFrom((byte)length));
				list.Add(4);
				int num = 0;
				for (int i = 0; i < list.Count; i++)
				{
					num += (int)list[i];
				}
				list.AddRange(SoftBasic.BuildAsciiBytesFrom((byte)num));
				result = OperateResult.CreateSuccessResult<byte[]>(list.ToArray());
			}
			return result;
		}

		/// <summary>
		/// One reading address Type of ReadByte
		/// </summary>
		/// <param name="station">plc station</param>
		/// <param name="address">address, for example: MX100, PX100</param>
		/// <param name="length">read length</param>
		/// <returns></returns>
		// Token: 0x06000A05 RID: 2565 RVA: 0x0003B71C File Offset: 0x0003991C
		public static OperateResult<byte[]> BuildReadOneCommand(byte station, string address, ushort length)
		{
			OperateResult<string> operateResult = XGBCnetOverTcp.AnalysisAddress(address);
			bool flag = !operateResult.IsSuccess;
			OperateResult<byte[]> result;
			if (flag)
			{
				result = OperateResult.CreateFailedResult<byte[]>(operateResult);
			}
			else
			{
				List<byte> list = new List<byte>();
				list.Add(5);
				list.AddRange(SoftBasic.BuildAsciiBytesFrom(station));
				list.Add(114);
				list.Add(83);
				list.Add(83);
				list.Add(48);
				list.Add(49);
				list.AddRange(SoftBasic.BuildAsciiBytesFrom((byte)operateResult.Content.Length));
				list.AddRange(Encoding.ASCII.GetBytes(operateResult.Content));
				list.Add(4);
				int num = 0;
				for (int i = 0; i < list.Count; i++)
				{
					num += (int)list[i];
				}
				list.AddRange(SoftBasic.BuildAsciiBytesFrom((byte)num));
				result = OperateResult.CreateSuccessResult<byte[]>(list.ToArray());
			}
			return result;
		}

		/// <summary>
		/// build read command. 
		/// </summary>
		/// <param name="station">station</param>
		/// <param name="address">start address</param>
		/// <param name="length">address length</param>
		/// <returns> command</returns>
		// Token: 0x06000A06 RID: 2566 RVA: 0x0003B814 File Offset: 0x00039A14
		public static OperateResult<byte[]> BuildReadCommand(byte station, string address, ushort length)
		{
			OperateResult<string> dataTypeToAddress = XGBFastEnet.GetDataTypeToAddress(address);
			bool flag = !dataTypeToAddress.IsSuccess;
			OperateResult<byte[]> result;
			if (flag)
			{
				result = OperateResult.CreateFailedResult<byte[]>(dataTypeToAddress);
			}
			else
			{
				string content = dataTypeToAddress.Content;
				string a = content;
				if (!(a == "Bit"))
				{
					if (!(a == "Word") && !(a == "DWord") && !(a == "LWord") && !(a == "Continuous"))
					{
						result = new OperateResult<byte[]>(StringResources.Language.NotSupportedDataType);
					}
					else
					{
						result = XGBCnetOverTcp.BuildReadByteCommand(station, address, length);
					}
				}
				else
				{
					result = XGBCnetOverTcp.BuildReadOneCommand(station, address, length);
				}
			}
			return result;
		}

		/// <summary>
		/// write data to address  Type of ReadByte
		/// </summary>
		/// <param name="station">plc station</param>
		/// <param name="address">address, for example: M100, D100, DW100</param>
		/// <param name="value">source value</param>
		/// <returns>command bytes</returns>
		// Token: 0x06000A07 RID: 2567 RVA: 0x0003B8B8 File Offset: 0x00039AB8
		public static OperateResult<byte[]> BuildWriteByteCommand(byte station, string address, byte[] value)
		{
			OperateResult<string> operateResult = XGBCnetOverTcp.AnalysisAddress(address);
			bool flag = !operateResult.IsSuccess;
			OperateResult<byte[]> result;
			if (flag)
			{
				result = OperateResult.CreateFailedResult<byte[]>(operateResult);
			}
			else
			{
				List<byte> list = new List<byte>();
				list.Add(5);
				list.AddRange(SoftBasic.BuildAsciiBytesFrom(station));
				list.Add(119);
				list.Add(83);
				list.Add(66);
				list.AddRange(SoftBasic.BuildAsciiBytesFrom((byte)operateResult.Content.Length));
				list.AddRange(Encoding.ASCII.GetBytes(operateResult.Content));
				list.AddRange(SoftBasic.BuildAsciiBytesFrom((byte)value.Length));
				list.AddRange(SoftBasic.BytesToAsciiBytes(value));
				list.Add(4);
				int num = 0;
				for (int i = 0; i < list.Count; i++)
				{
					num += (int)list[i];
				}
				list.AddRange(SoftBasic.BuildAsciiBytesFrom((byte)num));
				result = OperateResult.CreateSuccessResult<byte[]>(list.ToArray());
			}
			return result;
		}

		/// <summary>
		/// write data to address  Type of One
		/// </summary>
		/// <param name="station">plc station</param>
		/// <param name="address">address, for example: M100, D100, DW100</param>
		/// <param name="value">source value</param>
		/// <returns>command bytes</returns>
		// Token: 0x06000A08 RID: 2568 RVA: 0x0003B9BC File Offset: 0x00039BBC
		public static OperateResult<byte[]> BuildWriteOneCommand(byte station, string address, byte[] value)
		{
			OperateResult<string> operateResult = XGBCnetOverTcp.AnalysisAddress(address);
			bool flag = !operateResult.IsSuccess;
			OperateResult<byte[]> result;
			if (flag)
			{
				result = OperateResult.CreateFailedResult<byte[]>(operateResult);
			}
			else
			{
				List<byte> list = new List<byte>();
				list.Add(5);
				list.AddRange(SoftBasic.BuildAsciiBytesFrom(station));
				list.Add(119);
				list.Add(83);
				list.Add(83);
				list.Add(48);
				list.Add(49);
				list.AddRange(SoftBasic.BuildAsciiBytesFrom((byte)operateResult.Content.Length));
				list.AddRange(Encoding.ASCII.GetBytes(operateResult.Content));
				list.AddRange(SoftBasic.BytesToAsciiBytes(value));
				list.Add(4);
				int num = 0;
				for (int i = 0; i < list.Count; i++)
				{
					num += (int)list[i];
				}
				list.AddRange(SoftBasic.BuildAsciiBytesFrom((byte)num));
				result = OperateResult.CreateSuccessResult<byte[]>(list.ToArray());
			}
			return result;
		}

		/// <summary>
		/// write data to address  Type of ReadByte
		/// </summary>
		/// <param name="station">plc station</param>
		/// <param name="address">address, for example: M100, D100, DW100</param>
		/// <param name="value">source value</param>
		/// <returns>command bytes</returns>
		// Token: 0x06000A09 RID: 2569 RVA: 0x0003BAC0 File Offset: 0x00039CC0
		public static OperateResult<byte[]> BuildWriteCommand(byte station, string address, byte[] value)
		{
			OperateResult<string> dataTypeToAddress = XGBFastEnet.GetDataTypeToAddress(address);
			bool flag = !dataTypeToAddress.IsSuccess;
			OperateResult<byte[]> result;
			if (flag)
			{
				result = OperateResult.CreateFailedResult<byte[]>(dataTypeToAddress);
			}
			else
			{
				string content = dataTypeToAddress.Content;
				string a = content;
				if (!(a == "Bit"))
				{
					if (!(a == "Word") && !(a == "DWord") && !(a == "LWord") && !(a == "Continuous"))
					{
						result = new OperateResult<byte[]>(StringResources.Language.NotSupportedDataType);
					}
					else
					{
						result = XGBCnetOverTcp.BuildWriteByteCommand(station, address, value);
					}
				}
				else
				{
					result = XGBCnetOverTcp.BuildWriteOneCommand(station, address, value);
				}
			}
			return result;
		}

		/// <summary>
		/// Extract actual data form plc response
		/// </summary>
		/// <param name="response">response data</param>
		/// <param name="isRead">read</param>
		/// <returns>result</returns>
		// Token: 0x06000A0A RID: 2570 RVA: 0x0003BB64 File Offset: 0x00039D64
		public static OperateResult<byte[]> ExtractActualData(byte[] response, bool isRead)
		{
			OperateResult<byte[]> result;
			try
			{
				if (isRead)
				{
					bool flag = response[0] == 6;
					if (flag)
					{
						byte[] array = new byte[response.Length - 13];
						Array.Copy(response, 10, array, 0, array.Length);
						result = OperateResult.CreateSuccessResult<byte[]>(SoftBasic.AsciiBytesToBytes(array));
					}
					else
					{
						byte[] array2 = new byte[response.Length - 9];
						Array.Copy(response, 6, array2, 0, array2.Length);
						result = new OperateResult<byte[]>((int)BitConverter.ToUInt16(SoftBasic.AsciiBytesToBytes(array2), 0), "Data:" + SoftBasic.ByteToHexString(response));
					}
				}
				else
				{
					bool flag2 = response[0] == 6;
					if (flag2)
					{
						result = OperateResult.CreateSuccessResult<byte[]>(new byte[0]);
					}
					else
					{
						byte[] array3 = new byte[response.Length - 9];
						Array.Copy(response, 6, array3, 0, array3.Length);
						result = new OperateResult<byte[]>((int)BitConverter.ToUInt16(SoftBasic.AsciiBytesToBytes(array3), 0), "Data:" + SoftBasic.ByteToHexString(response));
					}
				}
			}
			catch (Exception ex)
			{
				result = new OperateResult<byte[]>(ex.Message);
			}
			return result;
		}
	}
}
